﻿using AutoMapper;
using MediatR;
using MyMES.Domain.W.Liststructure;
using MyMES.Domain.zmc;
using MyMES.Domain.zyy;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Commends.Productionorderss;
using MyMES.Read.API.Dtos;

namespace MyMES.Read.API.Application.CommendHandlers.Productionorderss
{
    public class GetProductiondetailsCommendHandlers : IRequestHandler<GetProductiondetailsCommends, APIResult<List<ProductiondetailsDtos>>>
    {
        private readonly IBaseRepository<woekderProess> workderRepository;
        private readonly IBaseRepository<Materialmanagement> materialmanagementRepository;
        private readonly IBaseRepository<WorkOrder> repository;
        private readonly IBaseRepository<Productionorders> production;
        private readonly IMapper mapper;

        public GetProductiondetailsCommendHandlers(IBaseRepository<woekderProess> workderRepository, IBaseRepository<Materialmanagement> materialmanagementRepository, IBaseRepository<WorkOrder> repository, IBaseRepository<Productionorders> production, IMapper mapper)
        {
            this.workderRepository = workderRepository;
            this.materialmanagementRepository = materialmanagementRepository;
            this.repository = repository;
            this.production = production;
            this.mapper = mapper;
        }



        /// <summary>
        /// 获取生产订单明细
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<List<ProductiondetailsDtos>>> Handle(GetProductiondetailsCommends request, CancellationToken cancellationToken)
        {
            APIResult<List<ProductiondetailsDtos>> result = new APIResult<List<ProductiondetailsDtos>>();
            result.Code = APIEnum.查询成功;
            result.Msg = "查询成功";

            string sql = $" SELECT a.*,b.Materialnumber,b.Materialname,b.Specification,b.Unit,b.InboundNumber,c.WorkOrderCoding,c.WorkOrderName,d.ProductionordersNo,d.ProductionordersName ,d.Head,d.Department ,d.ProductionordersStatus,d.PlanEndTime,d.PlanStartTime FROM woekderproess a LEFT JOIN materialmanagement b on a.MId=b.MId  LEFT JOIN workorder c on a.WorkOrderId=c.WorkOrderId  LEFT JOIN productionorders d on a.ProductionordersId=d.ProductionordersId    where woekderProessId= '{request.woekderProessId}' ";


            var workder = await workderRepository.SelectAsync(sql);
            var pro =await production.SelectAsync(sql);
            var work=await repository.SelectAsync(sql);
            var mater=await materialmanagementRepository.SelectAsync(sql);


            var workders = mapper.Map<List<ProductiondetailsDtos>>(workder);
            var pros=mapper.Map<List<ProductiondetailsDtos>>(pro);
            var works= mapper.Map<List<ProductiondetailsDtos>>(work);
            var maters= mapper.Map<List<ProductiondetailsDtos>>(mater);

            foreach (var item in workders)
            {
          
                item.ProductionordersNo=pros.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault()?.ProductionordersNo;
                item.ProductionordersName = pros.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault()?.ProductionordersName;
                item.Head = pros.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault()?.Head;
                item.yield = pros.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault()?.yield;
                item.Department = pros.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault()?.Department;

                item.ProductionordersStatus = pros.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault().ProductionordersStatus;
                item.PlanStartTime = pros.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault().PlanStartTime;
                item.PlanEndTime = pros.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault().PlanEndTime;

           

                item.WorkOrderCoding = works.Where(x => x.WorkOrderId == item.WorkOrderId).FirstOrDefault()?.WorkOrderCoding;
                item.OrderNumber = works.Where(x => x.WorkOrderId == item.WorkOrderId).FirstOrDefault()?.OrderNumber;

                item.Materialnumber = maters.Where(x => x.MId == item.MId).FirstOrDefault()?.Materialnumber;
                item.Materialname = maters.Where(x => x.MId == item.MId).FirstOrDefault()?.Materialname;
                item.Specification = maters.Where(x => x.MId == item.MId).FirstOrDefault()?.Specification;
                item.Unit = maters.Where(x => x.MId == item.MId).FirstOrDefault().Unit;
                item.Materialproduct = maters.Where(x => x.MId == item.MId).FirstOrDefault().Materialproduct;


            }

            result.data= workders;
            return result;
        }
    }
}
